<?php

define('S_DEBUG', 1); //是否是调试状态
define('S_ROOT', dirname(__FILE__)); //应用程序所在目录
define('IN_DIALOG', true); //应用程序标识
error_reporting(S_DEBUG ? E_ERROR : 0); //应用程序产生的错误输出
//初始化全局变量
$_SGLOBAL = $_SCONFIG = $_TPL = array();


include_once S_ROOT . '/config.inc.php';
include_once S_ROOT . '/include/global.func.php';

//初始化时间
$_SGLOBAL['timestamp'] = time();
$_SGLOBAL['m_timestamp'] = Utils::getMicroTime();

//模板相关设置
View::setReal(S_DEBUG);
View::setTemplateDir(S_ROOT . '/template/default');
View::setTmpDir(S_ROOT . '/data/tpl_cache');
$_SGLOBAL['view'] = new View();

//初始化数据库连接 
$_SGLOBAL['db'] = new DB();
$_SGLOBAL['db']->getConection($_SC['dsn']);
unset($_SC['dsn']);

//初始化日志记录器
$_SGLOBAL['logger'] = Logger::getLogger('dialogcn');
$_SGLOBAL['logger']->setConfig(array('level' => 'debug', 'file' => S_ROOT . '/logs/dialogcn.log'));

//初始化外部数据
if (!get_magic_quotes_gpc()) {
    foreach (array('_GET', '_POST', '_REQUEST') as $value) {
        foreach ($$value as &$gbc) {
            $gbc = Utils::saddslashes($gbc);
        }
    }
}

$_SGLOBAL['onlineip'] = Utils::getonlineip();
$_SGLOBAL['formhash'] = Utils::formhash();
$_SGLOBAL['inajax'] = isset($_GET['inajax']) ? 1 : 0;

//输出压缩
if ($_SC['gz'] && function_exists('ob_gzhandler')) {
    ob_start('ob_gzhandler');
}
header("Content-Type:text/html;charset={$_SC['charset']}");

//整站配置检查
if (!file_exists(S_ROOT . '/data/data_config.php')) {
    foreach (array('config', 'email') as $value) {
        Config::updateConfig($value);
    }
}

//加载全站配置文件
include_once S_ROOT . '/data/data_config.php';

date_default_timezone_set($_SCONFIG['timezone']);


if (isset($_SERVER['HTTP_REFERER'])) {
    $_SGLOBAL['refer'] = $_SERVER['HTTP_REFERER'];
} elseif (isset($_REQUEST['refer'])) {
    $_SGLOBAL['refer'] = $_REQUEST['refer'];
} else {
    $_SGLOBAL['refer'] = '';
}


//检查用户登陆状态
LoginAuth::checkLogin();
?>
